#include "account.h"
#include "ui_account.h"
#include <QValidator.h>
#include "database/usersdb.h"

Account::Account(QWidget *parent) :
    QWidget(parent),
    ui(new Ui::Account)
{
    ui->setupUi(this);
    this->setWindowFlags(Qt::Window);

    QRegExp regx("[a-zA-Z]+$");
    QValidator *validator = new QRegExpValidator(regx,  ui->lineEdit);
    ui->lineEdit->setValidator( validator);

    //设置背景图片
    QPalette palette = this->palette();
    palette.setBrush(this->backgroundRole(),QBrush(QPixmap(":/resources/redcar.jpg")));
    this->setPalette(palette);
}

Account::~Account()
{
    delete ui;
}

/**
 * @brief 显示account界面
 *
 */
void Account::accountShow(const QString &username)
{
    if (username.isEmpty())
    {
        this->show();
        return;
    }
    UsersDB *userdb = new UsersDB();
    int fontsize = userdb->getFontSize(username);
    //设置事例字体点阵
    QFont font;
    font.setPointSize(fontsize);
    this->ui->label_2->setFont(font);
    this->show();
}

/**
 * @brief 返回
 *
 */
void Account::on_pushButton_2_clicked()
{
    this->hide();
    emit cancel();
}

/**
 * @brief 确定
 *
 */
void Account::on_pushButton_clicked()
{
    UsersDB *userdb = new UsersDB();
    if ( !ui->lineEdit->text().isEmpty())
    {
        QString name = ui->lineEdit->text();
        if ( !userdb->isUserRepeat(name))
        {
            ui->label_2->setText(QString("用户%1已存在")
                                 .arg(name));
            ui->lineEdit->clear();
            return;
        }
        userdb->creatUserInfo(name);
        ui->lineEdit->clear();
        this->hide();
        emit cancel();
    }
    ui->label_2->setText("输入姓名为空~~");
}
